public class demo5 {
    public static Object lock=new Object();
    public static int i=0;
    public static int cur=1;
    public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            while(i<=200){
                synchronized (lock){
                    while(cur!=1){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("t1 "+i);
                    i++;
                    cur=2;
                    lock.notifyAll();
                }
            }
        });
        Thread t2=new Thread(()->{
            while(i<200){
                synchronized (lock){
                    while(cur!=2){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("t2 "+i);
                    i++;
                    cur=1;
                    lock.notifyAll();
                }
            }
        });
        t1.start();
        Thread.sleep(100);
        t2.start();
    }
}
